package com.china08.yxyapi.api.schwebsite.index;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.sws.SwsSection;
import com.china08.yxyapi.entity.db.sws.SwsSettingDomains;
import com.china08.yxyapi.entity.mg.sws.SwsArticle;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.SwsSectionRepository;
import com.china08.yxyapi.repository.db.SwsSettingDomainsRepository;
import com.china08.yxyapi.repository.mg.SwsArticleRepository;
import com.china08.yxyapi.util.DateToolUtils;

/**
 * 学校网站首页:热门咨询:文章列表
 * 
 * @author Ertao.Fang
 *
 */
@CrossOrigin
@RestController("SwsIndexHotArticleListController")
public class SwsIndexHotArticleListController extends YxyApiBaseController {

    @Autowired
    private SwsArticleRepository swsArticleRepository;

    @Autowired
    private SwsSectionRepository swsSectionRepository;

    @Autowired
    private SwsSettingDomainsRepository swsSettingDomainsRepository;

    @RequestMapping(value = "/api_yw/schwebsite/index/hot/article", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel) {

        String domainName = getDomainName(request);
        Assert.isTrue(StringUtils.isNotBlank(domainName), "域名为空");
        // 通过域名获取学校ID
        SwsSettingDomains byDomainName = swsSettingDomainsRepository.findByDomainName(domainName);
        Assert.isTrue(null != byDomainName, "域名对象为空");
        String schoolId = byDomainName.getSchoolId();
        boolean isVisible = true;
        boolean showInHomePage = true;
        boolean isOutLink = false;
        String pid = "0";
        List<SwsSection> list = swsSectionRepository
                .findBySchoolIdAndIsVisibleAndIsOutLinkAndShowInHomePageAndPidOrderByOrderNumAsc(schoolId, isVisible,
                        isOutLink, showInHomePage, pid);
        Assert.isTrue(null != list, "栏目为空");

        RespModel model = null;
        List<RespModel> respModels = new ArrayList<RespModel>();

        List<Order> orders = new ArrayList<Order>();
        orders.add(new Order(Direction.DESC, "isSticky"));
        orders.add(new Order(Direction.DESC, "showDate"));
        Integer auditStatus = 1;
        Sort sort = new Sort(orders);
        for (SwsSection swsSection : list) {
            model = new RespModel();
            model.sectionName = swsSection.getSectionName();
            model.section1Id = swsSection.getId();

            List<SwsArticle> list2 = swsArticleRepository.findTop5ByOwnerSchoolIdAndSection1IdAndAuditStatus(schoolId,
                    swsSection.getId(), auditStatus, sort);
            Article article = null;
            List<Article> articleList = new ArrayList<Article>();
            for (SwsArticle swsArticle : list2) {
                article = new Article();
                article.id = swsArticle.getId();
                article.title = swsArticle.getTitle();
                article.showDate = DateToolUtils.format(swsArticle.getShowDate(), "MM-dd");
                article.section1Id = swsArticle.getSection1Id();
                if (StringUtils.isBlank(swsArticle.getSection2Id())) {
                    article.section2Id = "0";
                } else {
                    article.section2Id = swsArticle.getSection2Id();
                }
                articleList.add(article);
            }
            model.articleList = articleList;
            respModels.add(model);
        }

        return respModels;

    }

    protected static class ReqModel extends YxyApiReqModel {

    }

    protected static class Article {

        public String id;

        public String section1Id;

        public String section2Id;

        public String title;

        public String showDate;
    }

    protected static class RespModel extends YxyApiRespModel {

        public String section1Id;

        public String sectionName;

        public List<Article> articleList;
    }

}
